package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * dish表现层
 */
@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    RedisTemplate<Object,Object> redisTemplate;

    /**
     * 添加菜品
     * @param dishDto
     * @return
     */
    @PostMapping
    @Transactional//存在多条sql语句操作,开启事务
    public R saveDish(@RequestBody DishDto dishDto){
        dishService.saveDish(dishDto);
        Set<Object> keys = redisTemplate.keys("dish*");
        redisTemplate.delete(keys);
        return R.success("添加成功");
    }

    /**
     * 分页查询菜品
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    @Transactional//存在多条sql语句操作,开启事务
    public R listDish(Integer page,Integer pageSize,String name){
        return R.success(dishService.listDish(page,pageSize,name));
    }

    /**
     * 根据id查询菜品数据，用于回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @Transactional//存在多条sql语句操作,开启事务
    public R getDishById(@PathVariable Long id){
        return R.success(dishService.getDishById(id));
    }

    /**
     * 修改菜品
     * @param dishDto
     * @return
     */
    @PutMapping
    @Transactional//存在多条sql语句操作,开启事务
    public R addDish(@RequestBody DishDto dishDto){
        dishService.updateDish(dishDto);
        redisTemplate.delete("dish_"+dishDto.getCategoryId());
        return R.success("添加成功");
    }

    /**
     * 删除菜品
     * @param ids
     * @return
     */
    @DeleteMapping
    public R deleteDish(@RequestParam List<Long> ids){
        dishService.deleteDish(ids);
        Set<Object> keys = redisTemplate.keys("dish*");
        redisTemplate.delete(keys);
        return R.success("删除成功");
    }

    /**
     * 起售停售菜品
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R changeStatus(@PathVariable Integer status,@RequestParam List<Long> ids){
        dishService.changeStatus(status,ids);
        return R.success("操作成功");
    }

    /**
     * 根据分类id查询该分类下的所有菜品集合
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    public R<List<DishDto>> getDishes(Long categoryId,Integer status){
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Dish::getCategoryId,categoryId)
                .eq(status!=null,Dish::getStatus,status);
        String dishNum = "dish_" + categoryId;
        List<DishDto> dishDtoList = (List<DishDto>) redisTemplate.opsForValue().get(dishNum);
        if(dishDtoList != null){
            return R.success(dishDtoList);
        }
        List<Dish> dishList = dishService.list(queryWrapper);
        dishDtoList = dishList.stream().map(dish -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish, dishDto);
            LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(DishFlavor::getDishId, dish.getId());
            List<DishFlavor> dishFlavors = dishFlavorService.list(lambdaQueryWrapper);
            dishDto.setFlavors(dishFlavors);
            return dishDto;
        }).collect(Collectors.toList());
        redisTemplate.opsForValue().set(dishNum,dishDtoList,1l, TimeUnit.DAYS);
        return R.success(dishDtoList);
    }
}
